package com.cx.controller.czw;

import com.cx.service.czw.UserPageInfoService;
import com.cx.vo.ResponseResult;
import com.cx.vo.UserDetailPageInfo;
import com.cx.vo.UserReceivePageInfo;
import com.cx.vo.UserSendPageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Controller
@RequestMapping("userPageInfo")
public class UserPageInfoController {
    @Autowired
    UserPageInfoService userPageInfoService;

    //点击查询时触发，页面加载时触发
    @RequestMapping("selectSendUserPageInfo")
    @ResponseBody
    public ResponseResult selectSendUserPageInfo(String phone ,String searchValue, HttpSession session){
        ResponseResult responseResult = new ResponseResult();
        //将登录时存的phone取出
        session.getAttribute("phone");
        //赋值给phone，根据phone来查寻信息
        phone = String.valueOf(session.getAttribute("phone"));
        System.out.println("查看我寄过的，电话 =" +phone);
        System.out.println("searchValue的值:" + searchValue);
        //如果传来的数据全为空就查询全部
        if(null == searchValue || "" == searchValue){
           List<UserSendPageInfo> userSendPageInfoList = userPageInfoService.selectSendPageInfo(phone);
            System.out.println(userSendPageInfoList);
           if(null != userSendPageInfoList){
               responseResult.setMessage("查询全部寄过的快递成功");
               responseResult.setCode(1);
               responseResult.setData(userSendPageInfoList);
           }else {
               responseResult.setMessage("查询全部寄过的快递失败");
               responseResult.setCode(2);
           }
        }else if(11 == searchValue.length()){
         //如果查询框中为11位，根据电话查找
            List<UserSendPageInfo> userSendPageInfoList = userPageInfoService.selectSendPageInfoByReceivePhone(phone ,searchValue);
            if(null != userSendPageInfoList){
                responseResult.setMessage("根据电话查询寄过的快递成功");
                responseResult.setCode(1);
                responseResult.setData(userSendPageInfoList);
            }else {
                responseResult.setMessage("根据电话查询寄过的快递失败");
                responseResult.setCode(2);
            }
        }else if(9 == searchValue.length()){
         //如果查询框中为15位，根据快递编号查找
            List<UserSendPageInfo> userSendPageInfoList = userPageInfoService.selectSendPageInfoByPageNum(phone ,searchValue);
            if(null != userSendPageInfoList){
                responseResult.setMessage("根据快递编号查询寄过的快递成功");
                responseResult.setCode(1);
                responseResult.setData(userSendPageInfoList);
            }else {
                responseResult.setMessage("根据快递编号查询寄过的快递失败");
                responseResult.setCode(2);
            }
        }
        return responseResult;
    }

    //点击查询时触发，页面加载时触发
    @RequestMapping("selectReceiveUserPageInfo")
    @ResponseBody
    public ResponseResult selectReceiveUserPageInfo(String phone ,String searchValue ,HttpSession session){
        ResponseResult responseResult = new ResponseResult();
        //将登录时存的phone取出
        session.getAttribute("phone");
        //赋值给phone，根据phone来查寻信息
        phone = String.valueOf(session.getAttribute("phone"));
        System.out.println("查看我收到的，电话 =" + phone);
        //如果传来的数据全为空就查询全部
        if(null == searchValue || "" == searchValue){
            List<UserReceivePageInfo> userReceivePageInfoList = userPageInfoService.selectReceivePageInfo(phone);
            if(null != userReceivePageInfoList){
                responseResult.setMessage("查询全部收到的快递成功");
                responseResult.setCode(1);
                responseResult.setData(userReceivePageInfoList);
            }else {
                responseResult.setMessage("没有信息");
                responseResult.setCode(2);
                System.out.println("结果集为 ：" +responseResult);
            }
        }else if(11 == searchValue.length()){
            //如果查询框中为11位，根据电话查找
            List<UserReceivePageInfo> userReceivePageInfoList = userPageInfoService.selectReceivePageInfoBySendPhone(phone ,searchValue);
            if(null != userReceivePageInfoList){
                responseResult.setMessage("根据电话查询收到的快递成功");
                responseResult.setCode(1);
                responseResult.setData(userReceivePageInfoList);
            }else {
                responseResult.setMessage("根据电话查询收到的快递失败");
                responseResult.setCode(2);
            }
        }else if(9 == searchValue.length()){
            //如果查询框中为9位，根据快递编号查找
            List<UserReceivePageInfo> userReceivePageInfoList = userPageInfoService.selectReceivePageInfoByPageNum(phone ,searchValue);
            if(null != userReceivePageInfoList){
                responseResult.setMessage("根据快递编号查询收到的快递成功");
                responseResult.setCode(1);
                responseResult.setData(userReceivePageInfoList);
            }else {
                responseResult.setMessage("根据快递编号查询收到的快递失败");
                responseResult.setCode(2);
            }
        }
        return responseResult;
    }

    //查询所有快递信息
    @RequestMapping("selectAllPageInfo")
    @ResponseBody
    public ResponseResult selectAllPageInfo(){
        ResponseResult responseResult = new ResponseResult();
        List<UserDetailPageInfo> userDetailPageInfoList = userPageInfoService.selectAllPageInfo();
        if(null != userDetailPageInfoList){
            responseResult.setMessage("查询全部快递成功");
            responseResult.setCode(1);
            responseResult.setData(userDetailPageInfoList);
        }else {
            responseResult.setMessage("查询全部快递失败");
            responseResult.setCode(2);
        }
        return responseResult;
    }
    //根据快递编号，或收件人电话或收件人姓名查询快递
    @RequestMapping("selectByConditions")
    @ResponseBody
    public ResponseResult selectByConditions(String searchValue){
        ResponseResult responseResult = new ResponseResult();
        String phoneRule = "^[\\d]{11}$";
        Pattern phonePattern = Pattern.compile(phoneRule);
        Matcher phoneMatcher = phonePattern.matcher(searchValue);

        String pageNumRule ="^[\\d]{9}$";
        Pattern pageNumPattern = Pattern.compile(pageNumRule);
        Matcher pageNumMatcher = pageNumPattern.matcher(searchValue);
        if(null == searchValue||"" == searchValue){
            List<UserDetailPageInfo> userDetailPageInfoList = userPageInfoService.selectAllPageInfo();
            if(null != userDetailPageInfoList){
                responseResult.setMessage("查询全部快递成功");
                responseResult.setCode(1);
                responseResult.setData(userDetailPageInfoList);
            }else {
                responseResult.setMessage("查询全部快递失败");
                responseResult.setCode(2);
            }
        }else if(phoneMatcher.matches()){
            List<UserDetailPageInfo> userDetailPageInfoList = userPageInfoService.selectPageByReceivePhone(searchValue);
            if(null != userDetailPageInfoList){
                responseResult.setMessage("根据收件人电话查询全部快递成功");
                responseResult.setCode(1);
                responseResult.setData(userDetailPageInfoList);
            }else {
                responseResult.setMessage("根据收件人电话查询全部快递失败");
                responseResult.setCode(2);
            }
        }else if(pageNumMatcher.matches()){
            List<UserDetailPageInfo> userDetailPageInfoList = userPageInfoService.selectPageByPageNum(Integer.parseInt(searchValue));
            if(null != userDetailPageInfoList){
                responseResult.setMessage("根据快递编号查询全部快递成功");
                responseResult.setCode(1);
                responseResult.setData(userDetailPageInfoList);
            }else {
                responseResult.setMessage("根据快递编号查询全部快递失败");
                responseResult.setCode(2);
            }
        }else {
            List<UserDetailPageInfo> userDetailPageInfoList = userPageInfoService.selectPageByReceiveName(searchValue);
            if(null != userDetailPageInfoList){
                responseResult.setMessage("根据收件人姓名查询全部快递成功");
                responseResult.setCode(1);
                responseResult.setData(userDetailPageInfoList);
            }else {
                responseResult.setMessage("根据收件人姓名查询全部快递失败");
                responseResult.setCode(2);
            }
        }
        return responseResult;
    }
    //点击详细信息触发
    @RequestMapping("userDetailPageInfo")
    @ResponseBody
    public ResponseResult userDetailPageInfo(Integer pageNum){
        ResponseResult responseResult = new ResponseResult();
        System.out.println("详情pageNum = " + pageNum);
        UserDetailPageInfo userDetailPageInfo = userPageInfoService.selectUserDetailPageInfo(pageNum);
        System.out.println(userDetailPageInfo);
        if(null != userDetailPageInfo){
            responseResult.setMessage("查询快递详细信息成功");
            responseResult.setCode(1);
            responseResult.setData(userDetailPageInfo);
        }else {
            responseResult.setMessage("查询快递详细信息失败");
            responseResult.setCode(2);
        }
        return  responseResult;
    }

    //已取消快递 的删除 delete
    @RequestMapping("deleteCancelPage")
    @ResponseBody
    public ResponseResult deleteCancelPage(Integer pageNum){
        ResponseResult responseResult = new ResponseResult();
        int affectRows =userPageInfoService.deleteCancelPage(pageNum);
        if(0 != affectRows){
            responseResult.setMessage("已取消快递删除成功");
            responseResult.setCode(1);
        }else {
            responseResult.setMessage("已取消快递删除失败");
            responseResult.setCode(2);
        }
        return responseResult;
    }

    //已下单快递 的取消 update
    @RequestMapping("cancelOrderPage")
    @ResponseBody
    public ResponseResult cancelOrderPage(Integer pageNum){
        ResponseResult responseResult = new ResponseResult();
        int affectRows =userPageInfoService.cancelOrderPage(pageNum);
        if(0 != affectRows){
            responseResult.setMessage("已下单快递取消成功");
            responseResult.setCode(1);
        }else {
            responseResult.setMessage("已下单快递取消失败");
            responseResult.setCode(2);
        }
        return responseResult;
    }
}
